import type { VbenFormSchema } from '#/adapter/form';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

import DictTag from '#/components/dict-tag/dict-tag.vue';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },

    {
      fieldName: 'brandModel',
      label: '品牌型号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入品牌型号',
      },
    },
    {
      fieldName: 'plateNumber',
      label: '车辆号牌',
      component: 'Input',
      componentProps: {
        placeholder: '请输入车辆号牌',
      },
    },
    {
      fieldName: 'purchaseDate',
      label: '购车时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'purchasePrice',
      label: '购车价格',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入购车价格',
        addonAfter: '万元',
      },
    },

    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '个人身份证号',
      field: 'personalIdcard',
    },
    {
      label: '品牌型号',
      field: 'brandModel',
    },
    {
      label: '车辆号牌',
      field: 'plateNumber',
    },
    {
      label: '购车时间',
      field: 'purchaseDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '购车价格',
      field: 'purchasePrice',
      render: (val) => `¥${val}`,
    },
    {
      label: '记录状态',
      field: 'status',
      render: (val) =>
        h(DictTag, { type: DICT_TYPE.COMMON_STATUS, value: val }),
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'brandModel',
      label: '品牌型号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入品牌型号',
      },
    },
    {
      fieldName: 'plateNumber',
      label: '车辆号牌',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入车辆号牌',
      },
    },
    {
      fieldName: 'purchaseDate',
      label: '购车时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerVehicleInfoApi.VehicleInfo>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'purchaseDate',
      title: '购车时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'purchasePrice',
      title: '购车价格',
      minWidth: 120,
    },
    {
      field: 'brandModel',
      title: '品牌型号',
      minWidth: 120,
    },
    {
      field: 'plateNumber',
      title: '车辆号牌',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
